package main

import (
	"bytes"
	"fmt"
	"github.com/bytedance/sonic/decoder"
	"github.com/bytedance/sonic/encoder"
	"strings"
)

// json序列化和反序列化：https://zhuanlan.zhihu.com/p/461772555
func main() {

	var o1 = map[string]interface{}{
		"a": "b",
	}
	var o2 = 1
	var w = bytes.NewBuffer(nil)
	var enc = encoder.NewStreamEncoder(w)
	enc.Encode(o1) // {"a":"b"}
	enc.Encode(o2)
	println(w.String()) // "{\"a\":\"b\"}\n1"

	// decpde test.
	var o = map[string]interface{}{}
	var r = strings.NewReader(`{"a":"b"}{"1":"2"}`)
	var dec = decoder.NewStreamDecoder(r)
	dec.Decode(&o)       // a:b
	dec.Decode(&o)       // map[1:2 a:b]
	fmt.Printf("%+v", o) // map[1:2 a:b]
}
